<template>
    <div class="goodsdetail-container">
        <transition 
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter">
            <div class="ball" id="shopball" v-show="flag" style="position: absolute; left: 152px; top: 327px;"></div>
        </transition>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <lbt-box :lunbotuList="lunbotu" :isfull="false"></lbt-box>
                </div>
            </div>
		</div>
        <div class="mui-card">
            <div class="mui-card-header">{{ goodsinfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        <span>市场价：<span class="old">￥{{ goodsinfo.market_price }}</span></span>&nbsp;&nbsp;
                        <span>销售价：<span class="now">￥{{ goodsinfo.sell_price }}</span></span>
                    </p>
                    <div class="num">购买数量：
                        <number :max="goodsinfo.stock_quantity" @getcount="getSelectedNum" id="ball-position"></number>
                    </div>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addShopcar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{ goodsinfo.goods_no }}</p>
                    <p>库存情况：{{ goodsinfo.stock_quantity }}件</p>
                    <p>上架时间：{{ goodsinfo.add_time | dataFormat }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDes(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
import lunbotuBox from "../subcomponent/lunbotu.vue"
import number from "../subcomponent/goods-numbox.vue"
export default {
    data(){
        return {
            id: this.$route.params.id,
            lunbotu: [],
            goodsinfo: {},
            flag: false,
            goodsnum: 1
        }
    },
    created(){
        this.getLunbotu()
        this.getGoodsInfo()
    },
    methods: {
        getLunbotu(){  // 获取轮播图图片
            this.$http.get('api/getthumimages/' + this.id).then(result => {
                if(result.body.status === 0){
                    result.body.message.forEach(item => {
                        item.img = item.src
                    })
                    this.lunbotu = result.body.message
                }
            })
        },
        getGoodsInfo(){  // 获取商品信息
            this.$http.get('api/goods/getinfo/' + this.id).then(result => {
                if(result.body.status === 0){
                    this.goodsinfo = result.body.message[0]
                }
            })
        },
        addShopcar(){   // 将货物添加入购物车
            this.flag = !this.flag
            var goodslist = {
                id: this.id,
                count: this.goodsnum,
                price: this.goodsinfo.sell_price,
                selected: false
            }
            this.$store.commit("setShopcar", goodslist)
        },
        getSelectedNum(count){    // 接收子组件传递的货物数量
            this.goodsnum = count
        },
        goDes(id){   // 使用编程式跳转到货物介绍页面
            this.$router.push({name: "goodsdes", params: {id}})
        },
        goComment(id){
            this.$router.push({name: "goodscomment", params: {id}})
        },
        beforeEnter(el){
            const startballPosition = document.getElementById("ball-position").getBoundingClientRect()
            el.style.left = startballPosition.left + 53 + "px"
            el.style.top = startballPosition.top + 15 +"px"
            el.style.transform = "translate(0, 0)"
        },
        enter(el, done){
            el.offsetWidth
            const returnPosition = document.getElementsByClassName("mui-badge")[0].getBoundingClientRect()
            const xDist = returnPosition.left
            const yDist = returnPosition.top
            const ballPosition = document.getElementById("shopball").getBoundingClientRect()
            const xBall = ballPosition.left
            const yBall = ballPosition.top
            const x = xDist - xBall
            const y = yDist - yBall
            el.style.transform = `translate(${x}px, ${y}px)`
            el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)";
            done()
        },
        afterEnter(el){
            el.style.opacity = 1
            this.flag = !this.flag
        }
    },
    components: {
        "lbt-box": lunbotuBox,
        number
    }
}
</script>

<style lang="scss" scoped>
.goodsdetail-container{
    .mui-card{
        font-weight: bold;
        .price{
            color: #333;
            .now{
                color: red;
            }
            .old{
                text-decoration: line-through;
            }
        }
        .num{
            margin-bottom: 5px;
        }
        .mui-card-footer{
            flex-wrap: wrap;
        }
    }
    .ball{
        width: 15px;
        height: 15px;
        background-color: red;
        border-radius: 50%;
        z-index: 99;
    }
}
</style>